package com.bolun998300.personal_blog_springboot.controller.background;


import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.bolun998300.personal_blog_springboot.Utils.Result;
import com.bolun998300.personal_blog_springboot.pojo.RecentUpdate;
import com.bolun998300.personal_blog_springboot.service.RecentUpdateService;

@RestController
public class RecentUpdateController {
	
	@Autowired
	private RecentUpdateService recentUpdateService;
	
	
	@GetMapping("/recentUpdates/listByPage")
	public HashMap<String, Object> listByPage(int page,int limit){
		Page<RecentUpdate> pageFromJPA = recentUpdateService.listByPage(page, limit);
		HashMap<String,Object> map = new HashMap<>();
		map.put("code",0);
        map.put("msg","");
        map.put("count",pageFromJPA.getTotalElements());
        map.put("data",pageFromJPA.getContent());
        return map;
	}
	
	
	@DeleteMapping("/recentUpdates/{id}")
	public Object delete(@PathVariable int id) {
		recentUpdateService.delete(id);
		return Result.success();
	}
	
	@GetMapping("/recentUpdates/{id}")
	public Object get(@PathVariable int id) {
		return recentUpdateService.get(id);
	}
	
	@PostMapping("/recentUpdates")
	public Object add(@RequestBody RecentUpdate bean) {
		recentUpdateService.add(bean);
		return Result.success();
	}
	
	
	@PutMapping("/recentUpdates")
	public Object update(@RequestBody RecentUpdate bean) {
		recentUpdateService.update(bean);
		return Result.success();
	}
	
	
	
	
	
}
